package com.wandousoushu.jiusen.booksource.model

import androidx.lifecycle.MutableLiveData
import com.wandousoushu.jiusen.booksource.bean.BookChapter
import java.util.ArrayList

class BookChapterManager {

    var bookChapterList = MutableLiveData<List<BookChapter>>()
    private val mBookChapters: MutableList<BookChapter> = ArrayList()

    companion object {
        private val instance = BookChapterManager()
        fun GetInstance(): BookChapterManager {
            return instance
        }
    }

    fun AddBookChapters(bookChapters: List<BookChapter>) {
        mBookChapters.clear()
        mBookChapters.addAll(bookChapters)
        bookChapterList.postValue(mBookChapters)
    }

    fun GetBookChaptersByUrl(bookUrl: String): List<BookChapter> {
        val newBookChapterList: MutableList<BookChapter> = ArrayList()
        for (bookChapter in mBookChapters) {
            if (bookChapter.bookUrl == bookUrl) {
                newBookChapterList.add(bookChapter)
            }
        }
        return newBookChapterList
    }

    fun GetBookChapterByUrl(bookUrl: String): BookChapter? {
        for (bookChapter in mBookChapters) {
            if (bookChapter.bookUrl == bookUrl) {
                return bookChapter
            }
        }
        return null
    }

    fun GetBookChapterCountByUrl(bookUrl: String): Int {
        val newBookChapterList: MutableList<BookChapter> = ArrayList()
        for (bookChapter in mBookChapters) {
            if (bookChapter.bookUrl == bookUrl) {
                newBookChapterList.add(bookChapter)
            }
        }
        return newBookChapterList.size
    }

    fun getBookChapter(bookUrl: String?, index: Int): BookChapter? {
        for (bookChapter in mBookChapters) {
            if (bookChapter.bookUrl.equals(bookUrl) && bookChapter.index == index) {
                return bookChapter
            }
        }
        return null
    }

    fun clear(){
        mBookChapters.clear()
        bookChapterList.postValue(mBookChapters)
    }

}